﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using SHOOTER.Managers;

namespace SHOOTER.Entities
{
    class Gunman
    { 
        Game mGame;
        MouseManager mMouseManager = MouseManager.Instance;

        SpriteBatch mSprite;
        Texture2D mTexture; //Cross hair reticle
        Texture2D mFlareTexture = null; // On shoot "effect" 4testing
        Vector2 mTexturePosition;// on shoot effect 4 testing

        public Vector2 mCrossPos;// Reticle position
        public Vector2 CrossPos { get { return mCrossPos; } set { mCrossPos = value; } }

        private uint mStartHealth = 100;
        public uint mCurrentHealth;
        
        private int mMaxAmmunition = 8;
        public int mCurrentAmmunition;

        private float fireRate = 1.1f;
        private float fireTime = 0f;

        private float mReload = 2f;
        private float mReloadTime = 0f;

        
      
       

        public  Gunman(GraphicsDevice device, Vector2 position, Game theGame, SpriteBatch mSpriteBatch){
            mGame = theGame;
            mSprite = mSpriteBatch;
            this.mCrossPos = position;
            this.mCurrentHealth = mStartHealth;
            this.mCurrentAmmunition = mMaxAmmunition;
            Initialize();
        }          

        private void Initialize()
        {
            mTexture = mGame.Content.Load<Texture2D>("Assets/Crosshairs");
            
            Console.WriteLine("Crosshairs Visible");
        }

        //Shoot function
        public void BANG()
        {
            if (mCurrentAmmunition <= 0)
            {
                mCurrentAmmunition = 0;
                Console.WriteLine("You Need To Reload");

            }
            if ((fireTime > fireRate) && (mCurrentAmmunition != 0))
            {
                Console.WriteLine("Bang");
                fireTime = 0f;
                mCurrentAmmunition--;
                CreateFlare();

               
            }
        }

        //Create muzzle flare on shoot.
        public void CreateFlare()
        {
            Vector2 mFlarePosition = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
            float flareTime = 0f;
            float flareFade = 0.8f;
            if (mMouseManager.MouseState.LeftButton == ButtonState.Pressed)
            {
                if (mFlareTexture == null)
                {
                    mFlareTexture = mGame.Content.Load<Texture2D>("Assets/Muzzleflare");
                }

                mTexturePosition = new Vector2(Mouse.GetState().X, Mouse.GetState().Y); 

         
            }
        }

        //Self Explanatory
        public void Reload()
        {
            if ((mCurrentAmmunition != 8) && (mReloadTime > mReload))
            {

                mCurrentAmmunition = 8;
                Console.WriteLine("OOKAAY... I'm Reloaded");
            }
            
    }

            

        public void Update(GameTime gameTime)
        {
            mCrossPos = new Vector2(Mouse.GetState().X, Mouse.GetState().Y); //Update Crosshair position
            fireTime += (float)gameTime.ElapsedGameTime.Milliseconds / 1000.0f; // increase fire time aka shoot cooldown
            mReloadTime += (float)gameTime.ElapsedGameTime.Milliseconds / 1000.0f;  

           if (mMouseManager.MouseState.LeftButton == ButtonState.Pressed && mMouseManager.MouseState.LeftButton != ButtonState.Released)
           { this.BANG(); }

           if (mMouseManager.MouseState.RightButton == ButtonState.Pressed && mMouseManager.MouseState.RightButton != ButtonState.Released)
           {
               this.Reload();
           }

           
        }

        public void Draw(GameTime gameTime)
        {
            mSprite.Begin();
            mSprite.Draw(mTexture, mCrossPos, Color.White);

            if (mFlareTexture != null)
            {
                mSprite.Draw(mFlareTexture, mTexturePosition, Color.White);
            }

            mSprite.End();

        }
    }
    
}
